package com.connector.server.server.listener;

import org.slf4j.LoggerFactory;
import com.connector.base.connection.Connection;
import com.connector.server.redis.OnLineUserCache;
import com.connector.server.util.PushMsgUtil;
import com.alibaba.fastjson.JSON;
import com.connector.server.util.SessionContextUtil;
import com.connector.base.message.model.BaseMessage;
import org.apache.commons.lang3.StringUtils;
import com.connector.base.utils.ErrorCode;
import com.google.common.base.Strings;
import com.connector.base.connection.channel.SocketChannel;
import com.connector.base.connection.channel.SocketIoSocketChannel;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.connector.base.connection.ConnectionRegistry;
import org.slf4j.Logger;
import com.connector.base.message.model.HandshakeMessage;
import com.corundumstudio.socketio.listener.DataListener;

public class SocketIoHandshakeListener implements DataListener<HandshakeMessage>
{
    private static final Logger log;
    private static final String SOCKETIO_EVENT_HANDSHAKE = "_handshakeEvent";
    private final ConnectionRegistry connectionRegistry;
    
    public SocketIoHandshakeListener(final ConnectionRegistry connectionRegistry) {
        this.connectionRegistry = connectionRegistry;
    }
    
    public void onData(final SocketIOClient socketIOClient, final HandshakeMessage handshakeMessage, final AckRequest ackRequest) throws Exception {
        final Connection connection = this.connectionRegistry.get((SocketChannel)new SocketIoSocketChannel(socketIOClient));
        if (Strings.isNullOrEmpty(handshakeMessage.getDeviceId())) {
            HandshakeMessage.buildErrorResponse(connection).setErrorCode(ErrorCode.INVALID_DEVICE).buildResult("Param invalid", "没有找到设备标识！").close();
            SocketIoHandshakeListener.log.error("握手失败,设备标识异常");
            return;
        }
        if (StringUtils.isEmpty((CharSequence)handshakeMessage.getUserId())) {
            HandshakeMessage.buildErrorResponse(connection).setErrorCode(ErrorCode.INVALID_DEVICE).buildResult("Param invalid", "用户id不能为空！").close();
            SocketIoHandshakeListener.log.error("握手失败,用户id不能为空");
            return;
        }
        try {
            BaseMessage.buildSuccessResponse(connection).send("_handshakeEvent");
            final String mobile = handshakeMessage.getMobile();
            if (StringUtils.isEmpty((CharSequence)mobile) || mobile.equals("1")) {
                handshakeMessage.setMobile("1");
            }
            else {
                handshakeMessage.setMobile("2");
            }
            connection.setSessionContext(SessionContextUtil.buildSessionContext(handshakeMessage));
            SocketIoHandshakeListener.log.info("握手成功,handshakeMessage:{}", (Object)JSON.toJSONString((Object)handshakeMessage));
            PushMsgUtil.pushHandshakeMsg(handshakeMessage.getUserId());
            OnLineUserCache.add(handshakeMessage.getUserId(), "1");
        }
        catch (Throwable e) {
            HandshakeMessage.buildErrorResponse(connection).setErrorCode(ErrorCode.UNKNOWN).buildResult(e.getMessage(), "握手失败！").close();
            SocketIoHandshakeListener.log.error("握手失败！", e);
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)SocketIoHandshakeListener.class);
    }
}
